#include "m0wig.h"
#include "ui_m0wig.h"
#include <map>
#include <bits/stdc++.h>
#include <iostream>
#include <string>

#define TOPIC_DATA_UP       "/app/data/up"//数据接收up modbus
#define TOPIC_DATA_DOWN     "/app/data/down"//命令控制down
#define TOPIC_CTL_UP        "/app/control/up"//数据接收up stm32
#define TOPIC_CTL_DOWN      "/app/control/down"//命令控制down

//设置stm32数值
M0Wig::M0Wig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::M0Wig)
{
    ui->setupUi(this);

    groupopen = new QButtonGroup(this);
    groupclose = new QButtonGroup(this);
    groupshangbao = new QButtonGroup(this);


    groupopen->addButton(ui->pushButtonopen,1);
    groupopen->addButton(ui->pushButtonclose,2);//将两个灯的开放到同一按钮组中，通过id方便发送消息

    groupshangbao->addButton(ui->radioButton_zhouqi,1);
    groupshangbao->addButton(ui->radioButton_shuaxin,2);
    groupshangbao->addButton(ui->radioButton_bianhua,3);

    connect(groupshangbao,SIGNAL(buttonClicked(int)),this,SLOT(radioButtonSlot(int)));
    connect(groupopen,SIGNAL(buttonClicked(int)),this,SLOT(btnclicklightSlot(int)));
    connect(ui->pushButton_shuaxin,SIGNAL(clicked()),this,SLOT(btnshuaxinSlot()));

    QString pathlightopen = ":/new/prefix1/picture_tubiao/lightopen.png";
    QString pathlightclose = ":/new/prefix1/picture_tubiao/lightclose.png";

    //QPixmap lightopen;
    lightopen.load(pathlightopen);

    //QPixmap lightclose;
    lightclose.load(pathlightclose);



}

void M0Wig::publish(QByteArray text)
{
    QMQTT::Message message;
    message.setTopic(TOPIC_DATA_DOWN);
    message.setPayload(text);

    qDebug() << "topic:" <<message.topic();
    qDebug() << "payload:"<<message.payload();

    emit dataDownSignal(message);
}

void M0Wig::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix(":/new/prefix2/picture_tubiao/beijing2.png");
    painter.drawPixmap(0, 0, pix);
}


M0Wig::~M0Wig()
{
    delete ui;
}


//数据接收
void M0Wig::dataUpSlot(const QByteArray &json)
{
    //qDebug()<<"stm32处理解析json语句";
    QJsonParseError jsonerror;//报告 JSON 处理过程中出现的错误
    //将json解析为utf-8的QJSON文档（读写 JSON 文档）
    QJsonDocument doucment = QJsonDocument::fromJson(json,&jsonerror);
    //判断文档是否有效性
    if (!doucment.isNull() && (jsonerror.error == QJsonParseError::NoError))
    {
        //查询一个文档是否包含了一个object
        if(doucment.isObject())
        {
            QJsonObject object = doucment.object();//封装对象
            if(object.contains("type")&&object.contains("result"))
            {
                QJsonValue value = object.value("type");
                int type = object.value("type").toInt();
                //qDebug()<<"1-采集回复指令";
                //qDebug()<<"type: "<< type;
                if(object.contains("data"))
                {
                    QJsonValue value= object.value("data");
                    if(value.isArray())
                    {
                        QJsonArray array = value.toArray();
                        //qDebug() << "data:" << array.size();
                        for(int i = 0; i < array.size(); i++)
                        {
                            //遍历数组
                            QJsonValue value = array.at(i);
                            if(value.isObject())
                            {
                                object = value.toObject();
                                int key = object.value("key").toInt();
                                QString val = object.value("val").toString();

                                if(key==3011)
                                {
                                    int val1=val.toFloat();
                                    ui->lcdNumber_wendu->display(val1);//此处为温度的值
                                }
                                else if(key==3012)
                                {
                                    int val2=val.toFloat();
                                    ui->lcdNumber_shidu->display(val2);//此处为湿度的值
                                }
                                else if(key==302)
                                {
                                    int val3 = val.toFloat();
                                    ui->progressBar_dian->setMaximum(100);
                                    ui->progressBar_dian->setValue(val3);//此处为电池电量
                                }
                                else if(key==303)
                                {
                                    int val4 =  val.toInt();
                                    if(val4 == 1)//开
                                    {
                                        ui->label_xiaoxikuang->clear();
                                        ui->label_xiaoxikuang->setStyleSheet(QString("border-image:url(:/new/prefix1/picture_tubiao/speak.png);"));
                                        ui->label_xiaoxikuang->setText(" 灯已打开");

                                        ui->label_light->clear();
                                        ui->label_light->setPixmap(lightopen);//灯开
                                    }
                                    else if(val4 == 0)//关
                                    {
                                        ui->label_xiaoxikuang->clear();
                                        ui->label_xiaoxikuang->setStyleSheet(QString("border-image:url(:/new/prefix1/picture_tubiao/speak.png);"));
                                        ui->label_xiaoxikuang->setText(" 灯已关闭");

                                        ui->label_light->clear();
                                        ui->label_light->setPixmap(lightclose);//灯关
                                    }
                                }
                                //stm32解析数据
                                qDebug() << "key:"<< key <<"val:" << val;
                            }
                        }
                    }
                }
            }
        }
    }
}

void M0Wig::btnclicklightSlot(int id)
{
    QJsonObject json;//创建一个空的 QJsonObject 对象,封装json对象
    QJsonDocument document;//读写json文档
    if(id==1)
    {
        ui->label_light->clear();
        ui->label_light->setPixmap(lightopen);//灯开

        ui->label_xiaoxikuang->clear();
        ui->label_xiaoxikuang->setStyleSheet(QString("border-image:url(:/new/prefix1/picture_tubiao/speak.png);"));
        ui->label_xiaoxikuang->setText(" 灯已打开");
        json.insert("type",2);
        QJsonObject data;
        data.insert("key",303);
        data.insert("val","1");
        json.insert("data",QJsonObject(data));

        document.setObject(json);//在QJsonDocument中创建一个与json相关联的json文档
        QByteArray byte_array = document.toJson(QJsonDocument::Compact);

        publish(byte_array);
        qDebug() << "灯已打开";

    }
    else if(id==2)
    {
        ui->label_light->clear();
        ui->label_light->setPixmap(lightclose);//灯关

        ui->label_xiaoxikuang->clear();
        ui->label_xiaoxikuang->setStyleSheet(QString("border-image:url(:/new/prefix1/picture_tubiao/speak.png);"));
        ui->label_xiaoxikuang->setText(" 灯已关闭");
        json.insert("type",2);
        QJsonObject data;
        data.insert("key",303);
        data.insert("val","0");
        json.insert("data",QJsonObject(data));

        document.setObject(json);//在QJsonDocument中创建一个与json相关联的json文档
        QByteArray byte_array = document.toJson(QJsonDocument::Compact);

        publish(byte_array);
        qDebug() << "灯已关闭";
    }
}

void M0Wig::radioButtonSlot(int id)
{
    QJsonObject json;//创建一个空的 QJsonObject 对象,封装json对象
    QJsonDocument document;//读写json文档
    if(id == 1)//周期
    {
        ui->label_xiaoxikuang->clear();
        ui->label_xiaoxikuang->setStyleSheet(QString("border-image:url(:/new/prefix1/picture_tubiao/speak.png);"));
        ui->label_xiaoxikuang->setText(" 当前为周期上报");

        int number = ui->spinBox->value();
        json.insert("type",3);
        QJsonObject data;
        data.insert("type",2);
        data.insert("period",number);
        json.insert("data",QJsonObject(data));

        document.setObject(json);//在QJsonDocument中创建一个与json相关联的json文档
        QByteArray byte_array = document.toJson(QJsonDocument::Compact);

        publish(byte_array);

        qDebug()<<"这是周期上报";

    }
    if(id == 2)//刷新
    {
        ui->label_xiaoxikuang->clear();
        ui->label_xiaoxikuang->setStyleSheet(QString("border-image:url(:/new/prefix1/picture_tubiao/speak.png);"));
        ui->label_xiaoxikuang->setText(" 当前为刷新上报");

        json.insert("type",3);
        QJsonObject data;
        data.insert("type",0);
        data.insert("period",2);
        json.insert("data",QJsonObject(data));

        document.setObject(json);//在QJsonDocument中创建一个与json相关联的json文档
        QByteArray byte_array = document.toJson(QJsonDocument::Compact);

        publish(byte_array);

        qDebug()<<"这是刷新上报";

    }
    else if(id == 3)//变化
    {
        ui->label_xiaoxikuang->clear();
        ui->label_xiaoxikuang->setStyleSheet(QString("border-image:url(:/new/prefix1/picture_tubiao/speak.png);"));
        ui->label_xiaoxikuang->setText(" 当前为变化上报");

        json.insert("type",3);
        QJsonObject data;
        data.insert("type",1);
        data.insert("period",3);
        json.insert("data",QJsonObject(data));

        document.setObject(json);//在QJsonDocument中创建一个与json相关联的json文档
        QByteArray byte_array = document.toJson(QJsonDocument::Compact);

        publish(byte_array);
        qDebug()<<"这是变化上报";

    }

}

void M0Wig::btnshuaxinSlot()
{
    ui->label_xiaoxikuang->clear();
    ui->label_xiaoxikuang->setStyleSheet(QString("border-image:url(:/new/prefix1/picture_tubiao/speak.png);"));
    ui->label_xiaoxikuang->setText(" 已刷新");


    QJsonObject json;//创建一个空的 QJsonObject 对象,封装json对象
    QJsonDocument document;//读写json文档

    json.insert("type",1);
    json.insert("limit","all");
    json.insert("keys",1);

    document.setObject(json);//在QJsonDocument中创建一个与json相关联的json文档
    QByteArray byte_array = document.toJson(QJsonDocument::Compact);

    publish(byte_array);
    qDebug()<<"刷新";
}
